<?php
namespace app\api\controller;

use app\facade\Basic;
use think\Request;
use app\api\service\Common as ServiceCommon;
use  app\api\traits\ValidateTrait;

class Common extends BaseController
{
    use ValidateTrait; // 使用 Trait
    protected $validationScenes  = [
        'translate'=>[
            'content'=>['require', 'translate.content'],
            'target_language'=>['require', 'translate.target_language'],
        ]
    ];
    /**
     *  公共常用函数
    */
    public function getEmailCode(Request $request)
    {
        $param['email'] = $request->param('email');
        $param['scene'] = $request->param('scene',1);

        /* 验证 */
        $this->validateData($param,'Common.getEmailCode');

        $ServiceCommon = new ServiceCommon();
        $result = $ServiceCommon->getEmailCode($param);
        return Basic::json($result);
    }

    /**
     * 批量翻译接口
     */
    public function translate(Request $request){
        $param = $request->all();
        try {
            $this->autoValidate('translate');
            // 验证通过逻辑...
        } catch (ValidateException $e) {
            // 错误处理...
            $customErrors = $e->getMessage()??'';
            return Basic::json(['code'=>100001, 'message'=>$customErrors ]);
        }
        $ServiceCommon = new ServiceCommon();
        $result = $ServiceCommon->translate($param);
        return Basic::json($result);

    }
}
